import { create } from 'axios';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import { Message } from 'element-ui';
const baseURL = process.env.VUE_APP_BASE_API
const service = create({
  baseURL,
  timeout: 5000,
});

service.interceptors.request.use(
  config => {
    NProgress.start();
    console.log(config)
    return config;
  },
  error => {
    console.log('error:', error);
    NProgress.done();
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  response => {
    NProgress.done()
    const res = response.data
    if (res.code !== 200) {
      Message({
        message: res.message || 'error',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  },
  error => {
    NProgress.done()
    console.log('err', error)
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
export default service
